<!DOCTYPE html><html><head><title>Ext.core.DomHelper | Ext JS 4.0 Documentation</title><script type="text/javascript" src="../ext-all.js"></script><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../scrollbars.css" type="text/css"><link rel="stylesheet" href="../docs.css" type="text/css"><link id="styleCss" rel="stylesheet" href="../style.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script><link rel="stylesheet" href="../prettify.css" type="text/css"><!-- link(rel: 'stylesheet', href: req.baseURL + '/css/ext4.css', type: 'text/css')--><link rel="shortcut icon" type="image/ico" href="../favicon.ico"><!--[if IE]>
<style type="text/css">.head-band { display: none; }
.header { border: 0; top: 0; left: 0px; background: url(../header.gif) repeat-x; }
.doc-tab .members .member a.more { background-color: #efefef; }
</style><link rel="stylesheet" href="/new/css/ie.css" type="text/css"><![endif]-->
</head><body id="ext-body" class="iScroll"><div id="notice" class="notice">For up to date documentation and features, visit 
<a href="http://docs.sencha.com/ext-js/4-0">http://docs.sencha.com/ext-js/4-0</a></div><div class="wrapper"><div class="head-band"></div><div class="header"><h2><a href="../index.html">Sencha Documentation</a></h2></div><div id="search"><form><input type="text" placeholder="Search" id="search-field" autocomplete="off" name="q"></form><div id="search-box"></div></div><div id="treePanel"></div><div id="container"><script type="text/javascript">

    req = {
        liveURL: '.',
        standAloneMode: true,
        origDocClass: 'Ext.core.DomHelper',
        docClass: 'Ext.core.DomHelper',
        docReq: 'Ext.core.DomHelper',
        version: '4.0',
        baseURL: '.',
        baseDocURL: '.',
        baseProdURL: '.'
    };

    clsInfo = {};



</script>

<script type="text/javascript" src="../search.js"></script>
<!--script type="text/javascript" src="/new/javascripts/app/examples.js"></script-->
<script type="text/javascript" src="../class_tree.js"></script>
<script type="text/javascript" src="../class_doc.js"></script>
<script type="text/javascript">
    req.source = 'DomHelper.html#Ext-core.DomHelper';
    clsInfo = {"methods":["append","applyStyles","createTemplate","insertAfter","insertBefore","insertFirst","insertHtml","markup","overwrite"],"cfgs":[],"properties":["createDom","useDom"],"events":[],"subclasses":[]};
    Ext.onReady(function() {
        Ext.create('Docs.classPanel');
    });
</script><div id="top-block" class="top-block"><h1 id="clsTitle" class="cls"><a href="../source/DomHelper.html#Ext-core.DomHelper" target="_blank">Ext.core.DomHelper</a></h1></div><div id="docContent"><div id="doc-overview-content"><div class="lft"><p>The DomHelper class provides a layer of abstraction from DOM and transparently supports creating
elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates
from your DOM building code.</p>




<p><b><u>DomHelper element specification object</u></b></p>


<p>A specification object is used when creating elements. Attributes of this object
are assumed to be element attributes, except for 4 special attributes:
<div class="mdetail-params"><ul>
<li><b><tt>tag</tt></b> : <div class="sub-desc">The tag name of the element</div></li>
<li><b><tt>children</tt></b> : or <tt>cn</tt><div class="sub-desc">An array of the
same kind of element definition objects to be created and appended. These can be nested
as deep as you want.</div></li>
<li><b><tt>cls</tt></b> : <div class="sub-desc">The class attribute of the element.
This will end up being either the "class" attribute on a HTML fragment or className
for a DOM node, depending on whether DomHelper is using fragments or DOM.</div></li>
<li><b><tt>html</tt></b> : <div class="sub-desc">The innerHTML for the element</div></li>
</ul></div></p>




<p><b><u>Insertion methods</u></b></p>


<p>Commonly used insertion methods:
<div class="mdetail-params"><ul>
<li><b><tt><a href="Ext.core.DomHelper.html#append" rel="Ext.core.DomHelper#append" class="docClass">append</a></tt></b> : <div class="sub-desc"></div></li>
<li><b><tt><a href="Ext.core.DomHelper.html#insertBefore" rel="Ext.core.DomHelper#insertBefore" class="docClass">insertBefore</a></tt></b> : <div class="sub-desc"></div></li>
<li><b><tt><a href="Ext.core.DomHelper.html#insertAfter" rel="Ext.core.DomHelper#insertAfter" class="docClass">insertAfter</a></tt></b> : <div class="sub-desc"></div></li>
<li><b><tt><a href="Ext.core.DomHelper.html#overwrite" rel="Ext.core.DomHelper#overwrite" class="docClass">overwrite</a></tt></b> : <div class="sub-desc"></div></li>
<li><b><tt><a href="Ext.core.DomHelper.html#createTemplate" rel="Ext.core.DomHelper#createTemplate" class="docClass">createTemplate</a></tt></b> : <div class="sub-desc"></div></li>
<li><b><tt><a href="Ext.core.DomHelper.html#insertHtml" rel="Ext.core.DomHelper#insertHtml" class="docClass">insertHtml</a></tt></b> : <div class="sub-desc"></div></li>
</ul></div></p>




<p><b><u>Example</u></b></p>


<p>This is an example, where an unordered list with 3 children items is appended to an existing
element with id <tt>'my-div'</tt>:<br>
 
<pre class="prettyprint"><code>var dh = Ext.core.DomHelper; // create shorthand alias
// specification object
var spec = {
    id: 'my-ul',
    tag: 'ul',
    cls: 'my-list',
    // append children after creating
    children: [     // may also specify 'cn' instead of 'children'
        {tag: 'li', id: 'item0', html: 'List Item 0'},
        {tag: 'li', id: 'item1', html: 'List Item 1'},
        {tag: 'li', id: 'item2', html: 'List Item 2'}
    ]
};
var list = dh.append(
    'my-div', // the context element 'my-div' can either be the id or the actual node
    spec      // the specification object
);
 </code></pre></p>


<p>Element creation specification parameters in this class may also be passed as an Array of
specification objects. This can be used to insert multiple sibling nodes into an existing
container very efficiently. For example, to add more list items to the example above:
<pre class="prettyprint"><code>dh.append('my-ul', [
    {tag: 'li', id: 'item3', html: 'List Item 3'},
    {tag: 'li', id: 'item4', html: 'List Item 4'}
]);
</code></pre></p>




<p><b><u>Templating</u></b></p>


<p>The real power is in the built-in templating. Instead of creating or appending any elements,
<tt><a href="Ext.core.DomHelper.html#createTemplate" rel="Ext.core.DomHelper#createTemplate" class="docClass">createTemplate</a></tt> returns a Template object which can be used over and over to
insert new elements. Revisiting the example above, we could utilize templating this time:
<pre class="prettyprint"><code>// create the node
var list = dh.append('my-div', {tag: 'ul', cls: 'my-list'});
// get template
var tpl = dh.createTemplate({tag: 'li', id: 'item{0}', html: 'List Item {0}'});

for(var i = 0; i < 5, i++){
    tpl.append(list, [i]); // use template to append to the actual node
}
</code></pre></p>
<p>An example using a template:
<pre class="prettyprint"><code>var html = '<a id="{0}" href="http://192.168.1.144:3000/new/ext-js/4-0/api/{1}" class="nav">{2}</a>';

var tpl = new Ext.core.DomHelper.createTemplate(html);
tpl.append('blog-roll', ['link1', 'http://www.edspencer.net/', "Ed&#39;s Site"]);
tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin&#39;s Site"]);
</code></pre></p>

<p>The same example using named parameters:
<pre class="prettyprint"><code>var html = '<a id="{id}" href="http://192.168.1.144:3000/new/ext-js/4-0/api/{url}" class="nav">{text}</a>';

var tpl = new Ext.core.DomHelper.createTemplate(html);
tpl.append('blog-roll', {
    id: 'link1',
    url: 'http://www.edspencer.net/',
    text: "Ed&#39;s Site"
});
tpl.append('blog-roll', {
    id: 'link2',
    url: 'http://www.dustindiaz.com/',
    text: "Dustin&#39;s Site"
});
</code></pre></p>

<p><b><u>Compiling Templates</u></b></p>
<p>Templates are applied using regular expressions. The performance is great, but if
you are adding a bunch of DOM elements using the same template, you can increase
performance even further by <a href="Ext.Template.html#compile" rel="Ext.Template#compile" class="docClass">&quot;compiling&quot;</a> the template.
The way "<a href="Ext.Template.html#compile" rel="Ext.Template#compile" class="docClass">compile()</a>" works is the template is parsed and
broken up at the different variable points and a dynamic function is created and eval'ed.
The generated function performs string concatenation of these parts and the passed
variables instead of using regular expressions.
<pre class="prettyprint"><code>var html = '<a id="{id}" href="http://192.168.1.144:3000/new/ext-js/4-0/api/{url}" class="nav">{text}</a>';

var tpl = new Ext.core.DomHelper.createTemplate(html);
tpl.compile();

//... use template like normal
</code></pre></p>

<p><b><u>Performance Boost</u></b></p>
<p>DomHelper will transparently create HTML fragments when it can. Using HTML fragments instead
of DOM can significantly boost performance.</p>
<p>Element creation specification parameters may also be strings. If <a href="Ext.core.DomHelper.html#useDom" rel="Ext.core.DomHelper#useDom" class="docClass">useDom</a> is <tt>false</tt>,
then the string is used as innerHTML. If <a href="Ext.core.DomHelper.html#useDom" rel="Ext.core.DomHelper#useDom" class="docClass">useDom</a> is <tt>true</tt>, a string specification
results in the creation of a text node. Usage:</p>
<pre class="prettyprint"><code>Ext.core.DomHelper.useDom = true; // force it to use DOM; reduces performance
</code></pre>

<div class="members"><div class="m-properties"><a name="properties"></a><div class="definedBy">Defined By</div><h3 class="prp p">Properties</h3><div id="property-createDom" class="member f ni"><a href="Ext.core.DomHelper.html#property-createDom" rel="property-createDom" class="expand more"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-property-createDom" class="viewSource">view source</a></div><a name="createDom"></a><a name="property-createDom"></a><a href="Ext.core.DomHelper.html#" rel="property-createDom" class="cls expand">createDom</a><span> : Object</span></div><div class="description"><div class="short"><p>Creates new DOM element(s) without inserting them to the document.</p>
</div><div class="long"><p>Creates new DOM element(s) without inserting them to the document.</p>
</div></div></div><div id="property-useDom" class="member ni"><a href="Ext.core.DomHelper.html#property-useDom" rel="property-useDom" class="expand more"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-property-useDom" class="viewSource">view source</a></div><a name="useDom"></a><a name="property-useDom"></a><a href="Ext.core.DomHelper.html#" rel="property-useDom" class="cls expand">useDom</a><span> : Boolean</span></div><div class="description"><div class="short"><p>True to force the use of DOM instead of html fragments</p>
</div><div class="long"><p>True to force the use of DOM instead of html fragments</p>
</div></div></div></div><div class="m-methods"><a name="methods"></a><div class="definedBy">Defined By</div><h3 class="mth p">Methods</h3><div id="method-append" class="member f ni"><a href="Ext.core.DomHelper.html#method-append" rel="method-append" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-append" class="viewSource">view source</a></div><a name="append"></a><a name="method-append"></a><a href="Ext.core.DomHelper.html#" rel="method-append" class="cls expand">append</a>(
<span class="pre">Mixed el, Object/String o, [Boolean returnElement]</span>)
 : HTMLElement/Ext.core.Element</div><div class="description"><div class="short"><p>Creates new DOM element(s) and appends them to el.</p>
</div><div class="long"><p>Creates new DOM element(s) and appends them to el.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : Mixed<div class="sub-desc"><p>The context element</p>
</div></li><li><span class="pre">o</span> : Object/String<div class="sub-desc"><p>The DOM object spec (and children) or raw HTML blob</p>
</div></li><li><span class="pre">returnElement</span> : Boolean<div class="sub-desc"><p>(optional) true to return a Ext.core.Element</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">HTMLElement/Ext.core.Element</span>&nbsp; &nbsp;<p>The new node</p>
</li></ul></div></div></div><div id="method-applyStyles" class="member ni"><a href="Ext.core.DomHelper.html#method-applyStyles" rel="method-applyStyles" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-applyStyles" class="viewSource">view source</a></div><a name="applyStyles"></a><a name="method-applyStyles"></a><a href="Ext.core.DomHelper.html#" rel="method-applyStyles" class="cls expand">applyStyles</a>(
<span class="pre">String/HTMLElement el, String/Object/Function styles</span>)
 : void</div><div class="description"><div class="short"><p>Applies a style specification to an element.</p>
</div><div class="long"><p>Applies a style specification to an element.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : String/HTMLElement<div class="sub-desc"><p>The element to apply styles to</p>
</div></li><li><span class="pre">styles</span> : String/Object/Function<div class="sub-desc"><p>A style specification string e.g. 'width:100px', or object in the form {width:'100px'}, or
a function which returns such a specification.</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
</li></ul></div></div></div><div id="method-createTemplate" class="member ni"><a href="Ext.core.DomHelper.html#method-createTemplate" rel="method-createTemplate" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-createTemplate" class="viewSource">view source</a></div><a name="createTemplate"></a><a name="method-createTemplate"></a><a href="Ext.core.DomHelper.html#" rel="method-createTemplate" class="cls expand">createTemplate</a>(
<span class="pre">Object o</span>)
 : Ext.Template</div><div class="description"><div class="short"><p>Creates a new <a href="Ext.Template.html" rel="Ext.Template" class="docClass">Ext.Template</a> from the DOM object spec.</p>
</div><div class="long"><p>Creates a new <a href="Ext.Template.html" rel="Ext.Template" class="docClass">Ext.Template</a> from the DOM object spec.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">o</span> : Object<div class="sub-desc"><p>The DOM object spec (and children)</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Ext.Template</span>&nbsp; &nbsp;<p>The new template</p>
</li></ul></div></div></div><div id="method-insertAfter" class="member ni"><a href="Ext.core.DomHelper.html#method-insertAfter" rel="method-insertAfter" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-insertAfter" class="viewSource">view source</a></div><a name="insertAfter"></a><a name="method-insertAfter"></a><a href="Ext.core.DomHelper.html#" rel="method-insertAfter" class="cls expand">insertAfter</a>(
<span class="pre">Mixed el, Object o, [Boolean returnElement]</span>)
 : HTMLElement/Ext.core.Element</div><div class="description"><div class="short"><p>Creates new DOM element(s) and inserts them after el.</p>
</div><div class="long"><p>Creates new DOM element(s) and inserts them after el.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : Mixed<div class="sub-desc"><p>The context element</p>
</div></li><li><span class="pre">o</span> : Object<div class="sub-desc"><p>The DOM object spec (and children)</p>
</div></li><li><span class="pre">returnElement</span> : Boolean<div class="sub-desc"><p>(optional) true to return a Ext.core.Element</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">HTMLElement/Ext.core.Element</span>&nbsp; &nbsp;<p>The new node</p>
</li></ul></div></div></div><div id="method-insertBefore" class="member ni"><a href="Ext.core.DomHelper.html#method-insertBefore" rel="method-insertBefore" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-insertBefore" class="viewSource">view source</a></div><a name="insertBefore"></a><a name="method-insertBefore"></a><a href="Ext.core.DomHelper.html#" rel="method-insertBefore" class="cls expand">insertBefore</a>(
<span class="pre">Mixed el, Object/String o, [Boolean returnElement]</span>)
 : HTMLElement/Ext.core.Element</div><div class="description"><div class="short"><p>Creates new DOM element(s) and inserts them before el.</p>
</div><div class="long"><p>Creates new DOM element(s) and inserts them before el.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : Mixed<div class="sub-desc"><p>The context element</p>
</div></li><li><span class="pre">o</span> : Object/String<div class="sub-desc"><p>The DOM object spec (and children) or raw HTML blob</p>
</div></li><li><span class="pre">returnElement</span> : Boolean<div class="sub-desc"><p>(optional) true to return a Ext.core.Element</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">HTMLElement/Ext.core.Element</span>&nbsp; &nbsp;<p>The new node</p>
</li></ul></div></div></div><div id="method-insertFirst" class="member ni"><a href="Ext.core.DomHelper.html#method-insertFirst" rel="method-insertFirst" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-insertFirst" class="viewSource">view source</a></div><a name="insertFirst"></a><a name="method-insertFirst"></a><a href="Ext.core.DomHelper.html#" rel="method-insertFirst" class="cls expand">insertFirst</a>(
<span class="pre">Mixed el, Object/String o, [Boolean returnElement]</span>)
 : HTMLElement/Ext.core.Element</div><div class="description"><div class="short"><p>Creates new DOM element(s) and inserts them as the first child of el.</p>
</div><div class="long"><p>Creates new DOM element(s) and inserts them as the first child of el.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : Mixed<div class="sub-desc"><p>The context element</p>
</div></li><li><span class="pre">o</span> : Object/String<div class="sub-desc"><p>The DOM object spec (and children) or raw HTML blob</p>
</div></li><li><span class="pre">returnElement</span> : Boolean<div class="sub-desc"><p>(optional) true to return a Ext.core.Element</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">HTMLElement/Ext.core.Element</span>&nbsp; &nbsp;<p>The new node</p>
</li></ul></div></div></div><div id="method-insertHtml" class="member ni"><a href="Ext.core.DomHelper.html#method-insertHtml" rel="method-insertHtml" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-insertHtml" class="viewSource">view source</a></div><a name="insertHtml"></a><a name="method-insertHtml"></a><a href="Ext.core.DomHelper.html#" rel="method-insertHtml" class="cls expand">insertHtml</a>(
<span class="pre">String where, HTMLElement/TextNode el, String html</span>)
 : HTMLElement</div><div class="description"><div class="short"><p>Inserts an HTML fragment into the DOM.</p>
</div><div class="long"><p>Inserts an HTML fragment into the DOM.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">where</span> : String<div class="sub-desc"><p>Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.</p>
</div></li><li><span class="pre">el</span> : HTMLElement/TextNode<div class="sub-desc"><p>The context element</p>
</div></li><li><span class="pre">html</span> : String<div class="sub-desc"><p>The HTML fragment</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">HTMLElement</span>&nbsp; &nbsp;<p>The new node</p>
</li></ul></div></div></div><div id="method-markup" class="member ni"><a href="Ext.core.DomHelper.html#method-markup" rel="method-markup" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-markup" class="viewSource">view source</a></div><a name="markup"></a><a name="method-markup"></a><a href="Ext.core.DomHelper.html#" rel="method-markup" class="cls expand">markup</a>(
<span class="pre">Object o</span>)
 : String</div><div class="description"><div class="short"><p>Returns the markup for the passed Element(s) config.</p>
</div><div class="long"><p>Returns the markup for the passed Element(s) config.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">o</span> : Object<div class="sub-desc"><p>The DOM object spec (and children)</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">String</span>&nbsp; &nbsp;
</li></ul></div></div></div><div id="method-overwrite" class="member ni"><a href="Ext.core.DomHelper.html#method-overwrite" rel="method-overwrite" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.core.DomHelper.html" class="definedIn docClass">Ext.core.DomHelper</a><br/><a href="../source/DomHelper.html#Ext-core.DomHelper-method-overwrite" class="viewSource">view source</a></div><a name="overwrite"></a><a name="method-overwrite"></a><a href="Ext.core.DomHelper.html#" rel="method-overwrite" class="cls expand">overwrite</a>(
<span class="pre">Mixed el, Object/String o, [Boolean returnElement]</span>)
 : HTMLElement/Ext.core.Element</div><div class="description"><div class="short"><p>Creates new DOM element(s) and overwrites the contents of el with them.</p>
</div><div class="long"><p>Creates new DOM element(s) and overwrites the contents of el with them.</p>
<h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : Mixed<div class="sub-desc"><p>The context element</p>
</div></li><li><span class="pre">o</span> : Object/String<div class="sub-desc"><p>The DOM object spec (and children) or raw HTML blob</p>
</div></li><li><span class="pre">returnElement</span> : Boolean<div class="sub-desc"><p>(optional) true to return a Ext.core.Element</p>
</div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">HTMLElement/Ext.core.Element</span>&nbsp; &nbsp;<p>The new node</p>
</li></ul></div></div></div></div></div></div></div><div id="pageContent"></div></div></div></div></body></html>